namespace CIS
{
    using CISLib;
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class PreferenceManage : Page
    {
        protected DropDownList AreaDdl;
        protected Image iPreManage;
        protected Label lbArea;
        protected Label lbpreLevelCn;
        protected Label lbpreLevelDdl;
        protected Label lbpreLevelEn;
        protected Label lbpreTypeAdd;
        protected Label lbpreTypeCn;
        protected Label lbpreTypeDdl;
        protected Label lbpreTypeEn;
        protected Label lbPreValueAdd;
        protected Label lbpreValueCn;
        protected Label lbpreValueDdl;
        protected Label lbpreValueEn;
        protected Label lbpreValueLevelAdd;
        protected Button preTypeAddBtn;
        protected TextBox preTypeAddEnTxb;
        protected TextBox preTypeAddTxb;
        protected DropDownList preTypeDdl;
        protected Button preTypeDelBtn;
        protected Button preTypeEditBtn;
        protected Button preValueAddBtn;
        protected TextBox preValueAddEnTxb;
        protected TextBox preValueAddTxb;
        protected DropDownList preValueDdl;
        protected Button preValueDelBtn;
        protected Button preValueEditBtn;
        protected Button preValueLevelAddBtn;
        protected TextBox preValueLevelAddEnTxb;
        protected TextBox preValueLevelAddTxb;
        protected DropDownList preValueLevelDdl;
        protected Button preValueLevelDelBtn;
        protected Button preValueLevelEditBtn;
        protected UserPrincipal Principal = null;

        private void AreaDdl_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.preTypeDdl.DataSource = ManagePreference.GetPreferenceType(new Guid(this.AreaDdl.SelectedValue));
            this.preTypeDdl.DataTextField = "PreferenceNameCn";
            this.preTypeDdl.DataValueField = "PreferenceId";
            this.preTypeDdl.DataBind();
            this.FillPreferenceValue(new Guid(this.preTypeDdl.SelectedItem.Value));
            this.FillPreferenceLevel(new Guid(this.preTypeDdl.SelectedItem.Value));
            this.ClearAllTxb();
        }

        private void ClearAllTxb()
        {
            this.preTypeAddTxb.Text = string.Empty;
            this.preTypeAddEnTxb.Text = string.Empty;
            this.preValueAddTxb.Text = string.Empty;
            this.preValueAddEnTxb.Text = string.Empty;
            this.preValueLevelAddTxb.Text = string.Empty;
            this.preValueLevelAddEnTxb.Text = string.Empty;
        }

        private void FillPreferenceLevel(Guid preferenceTypeId)
        {
            this.preValueLevelDdl.DataSource = ManagePreference.GetPreferenceLevel(preferenceTypeId);
            this.preValueLevelDdl.DataTextField = "PreferenceLevelNameCn";
            this.preValueLevelDdl.DataValueField = "PreferenceLevelId";
            this.preValueLevelDdl.DataBind();
        }

        private void FillPreferenceValue(Guid preferenceTypeId)
        {
            this.preValueDdl.DataSource = ManagePreference.GetPreferenceValue(preferenceTypeId);
            this.preValueDdl.DataTextField = "PreferenceValueNameCn";
            this.preValueDdl.DataValueField = "PreferenceValueId";
            this.preValueDdl.DataBind();
        }

        private void InitializeComponent()
        {
            this.AreaDdl.SelectedIndexChanged += new EventHandler(this.AreaDdl_SelectedIndexChanged);
            this.preTypeAddBtn.Click += new EventHandler(this.preTypeAddBtn_Click);
            this.preTypeEditBtn.Click += new EventHandler(this.preTypeEditBtn_Click);
            this.preTypeDelBtn.Click += new EventHandler(this.preTypeDelBtn_Click);
            this.preTypeDdl.SelectedIndexChanged += new EventHandler(this.preTypeDdl_SelectedIndexChanged);
            this.preValueAddBtn.Click += new EventHandler(this.preValueAddBtn_Click);
            this.preValueEditBtn.Click += new EventHandler(this.preValueEditBtn_Click);
            this.preValueDelBtn.Click += new EventHandler(this.preValueDelBtn_Click);
            this.preValueDdl.SelectedIndexChanged += new EventHandler(this.preValueDdl_SelectedIndexChanged);
            this.preValueLevelAddBtn.Click += new EventHandler(this.preValueLevelAddBtn_Click);
            this.preValueLevelEditBtn.Click += new EventHandler(this.preValueLevelEditBtn_Click);
            this.preValueLevelDelBtn.Click += new EventHandler(this.preValueLevelDelBtn_Click);
            this.preValueLevelDdl.SelectedIndexChanged += new EventHandler(this.preValueLevelDdl_SelectedIndexChanged);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            this.SetFrom();
            try
            {
                Area[] area = ManageArea.GetAreaAryViaId(this.Principal.SysUser.UserId);
                this.AreaDdl.Items.Clear();
                for (int i = 0; i < area.Length; i++)
                {
                    ListItem tmpListItem = new ListItem();
                    tmpListItem.Text = area[i].AreaName;
                    tmpListItem.Value = area[i].AreaId.ToString();
                    this.AreaDdl.Items.Add(tmpListItem);
                }
                this.preTypeDdl.DataSource = ManagePreference.GetPreferenceType(new Guid(this.AreaDdl.SelectedValue));
                this.preTypeDdl.DataTextField = "PreferenceNameCn";
                this.preTypeDdl.DataValueField = "PreferenceId";
                this.preTypeDdl.DataBind();
                this.FillPreferenceValue(new Guid(this.preTypeDdl.SelectedItem.Value));
                this.FillPreferenceLevel(new Guid(this.preTypeDdl.SelectedItem.Value));
            }
            catch (Exception)
            {
            }
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (this.Principal.SysUser.IsAdmin == 0)
            {
                throw new ApplicationException(string.Format("User: {0} not has privilege to access this page.", this.Principal.SysUser.LoginName));
            }
            if (!base.IsPostBack)
            {
                this.InitPage();
                this.SetFromImage();
            }
        }

        private void preTypeAddBtn_Click(object sender, EventArgs e)
        {
            string strPreTypeCn = this.preTypeAddTxb.Text.Trim();
            string strPreTypeEn = this.preTypeAddEnTxb.Text.Trim();
            if (!(strPreTypeCn == string.Empty) && !(strPreTypeEn == string.Empty))
            {
                ManagePreference.InsertNewPreferenceType(strPreTypeCn, strPreTypeEn, new Guid(this.AreaDdl.SelectedValue));
                this.InitPage();
                this.ClearAllTxb();
            }
        }

        private void preTypeDdl_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.FillPreferenceValue(new Guid(this.preTypeDdl.SelectedItem.Value));
            this.FillPreferenceLevel(new Guid(this.preTypeDdl.SelectedItem.Value));
            PreferenceKind preferenceKind = ManagePreference.GetPreferenceTypeViaId(new Guid(this.preTypeDdl.SelectedValue));
            this.preTypeAddTxb.Text = preferenceKind.PreferenceNameCn;
            this.preTypeAddEnTxb.Text = preferenceKind.PreferenceNameEn;
        }

        private void preTypeDelBtn_Click(object sender, EventArgs e)
        {
            try
            {
                Guid preId = new Guid(this.preTypeDdl.SelectedValue);
                if (ManagePreference.IsExistPreferenceIdInPreferenceLevel(preId) || ManagePreference.IsExistPreferenceIdInPreferenceValue(preId))
                {
                    base.Response.Write("<script> alert('尚有关联数据未删除！') </script>");
                }
                else
                {
                    ManagePreference.RemovePreferenceTypeViaId(preId);
                    this.InitPage();
                }
            }
            catch (Exception)
            {
            }
        }

        private void preTypeEditBtn_Click(object sender, EventArgs e)
        {
            string strPreTypeCn = this.preTypeAddTxb.Text.Trim();
            string strPreTypeEn = this.preTypeAddEnTxb.Text.Trim();
            if (!(strPreTypeCn == string.Empty) && !(strPreTypeEn == string.Empty))
            {
                ManagePreference.UpdatePreferenceType(new Guid(this.preTypeDdl.SelectedValue), strPreTypeCn, strPreTypeEn);
                this.InitPage();
                this.ClearAllTxb();
            }
        }

        private void preValueAddBtn_Click(object sender, EventArgs e)
        {
            string strPreValueCn = this.preValueAddTxb.Text.Trim();
            string strPreValueEn = this.preValueAddEnTxb.Text.Trim();
            if (!(strPreValueCn == string.Empty) && !(strPreValueEn == string.Empty))
            {
                ManagePreference.InsertNewPreferenceValue(new Guid(this.preTypeDdl.SelectedItem.Value), strPreValueCn, strPreValueEn);
                this.FillPreferenceValue(new Guid(this.preTypeDdl.SelectedItem.Value));
                this.ClearAllTxb();
            }
        }

        private void preValueDdl_SelectedIndexChanged(object sender, EventArgs e)
        {
            PreferenceValue preferenceValue = ManagePreference.GetPreferenceValueViaId(new Guid(this.preValueDdl.SelectedValue));
            this.preValueAddTxb.Text = preferenceValue.PreferenceValueNameCn;
            this.preValueAddEnTxb.Text = preferenceValue.PreferenceValueNameEn;
        }

        private void preValueDelBtn_Click(object sender, EventArgs e)
        {
            try
            {
                ManagePreference.RemoveCustomerPreferenceValueViaId(new Guid(this.preValueDdl.SelectedValue));
                this.InitPage();
            }
            catch (Exception)
            {
            }
        }

        private void preValueEditBtn_Click(object sender, EventArgs e)
        {
            string strPreValueCn = this.preValueAddTxb.Text.Trim();
            string strPreValueEn = this.preValueAddEnTxb.Text.Trim();
            if (!(strPreValueCn == string.Empty) && !(strPreValueEn == string.Empty))
            {
                ManagePreference.UpdatePreferenceValue(new Guid(this.preValueDdl.SelectedValue), strPreValueCn, strPreValueEn);
                this.FillPreferenceValue(new Guid(this.preTypeDdl.SelectedItem.Value));
                this.ClearAllTxb();
            }
        }

        private void preValueLevelAddBtn_Click(object sender, EventArgs e)
        {
            string strPreLevelCn = this.preValueLevelAddTxb.Text.Trim();
            string strPreLevelEn = this.preValueLevelAddEnTxb.Text.Trim();
            if (!(strPreLevelCn == string.Empty) && !(strPreLevelEn == string.Empty))
            {
                ManagePreference.InsertNewPreferenceLevel(new Guid(this.preTypeDdl.SelectedItem.Value), strPreLevelCn, strPreLevelEn);
                this.FillPreferenceLevel(new Guid(this.preTypeDdl.SelectedItem.Value));
                this.ClearAllTxb();
            }
        }

        private void preValueLevelDdl_SelectedIndexChanged(object sender, EventArgs e)
        {
            PreferenceValueLevel preferenceValueLevel = ManagePreference.GetPreferenceValueLevelViaId(new Guid(this.preValueLevelDdl.SelectedValue));
            this.preValueLevelAddTxb.Text = preferenceValueLevel.PreferenceValueLevelNameCn;
            this.preValueLevelAddEnTxb.Text = preferenceValueLevel.PreferenceValueLevelNameEn;
        }

        private void preValueLevelDelBtn_Click(object sender, EventArgs e)
        {
            try
            {
                ManagePreference.RemoveCustomerPreferenceLevelViaId(new Guid(this.preValueLevelDdl.SelectedValue));
                this.InitPage();
            }
            catch (Exception)
            {
            }
        }

        private void preValueLevelEditBtn_Click(object sender, EventArgs e)
        {
            string strPreLevelCn = this.preValueLevelAddTxb.Text.Trim();
            string strPreLevelEn = this.preValueLevelAddEnTxb.Text.Trim();
            if (!(strPreLevelCn == string.Empty) && !(strPreLevelEn == string.Empty))
            {
                ManagePreference.UpdatePreferenceValueLevel(new Guid(this.preValueLevelDdl.SelectedValue), strPreLevelCn, strPreLevelEn);
                this.FillPreferenceLevel(new Guid(this.preTypeDdl.SelectedItem.Value));
                this.ClearAllTxb();
            }
        }

        private void SetFrom()
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/CIS/LanguageConfig.xml");
            this.lbpreTypeAdd.Text = ManageSystemConfig.GetSystemSetLanguage("PreferenceManage", "lbpreTypeAdd", language, path);
            this.lbpreTypeCn.Text = ManageSystemConfig.GetSystemSetLanguage("PreferenceManage", "lbpreTypeCn", language, path);
            this.lbpreTypeEn.Text = ManageSystemConfig.GetSystemSetLanguage("PreferenceManage", "lbpreTypeEn", language, path);
            this.lbpreTypeDdl.Text = ManageSystemConfig.GetSystemSetLanguage("PreferenceManage", "lbpreTypeDdl", language, path);
            this.lbPreValueAdd.Text = ManageSystemConfig.GetSystemSetLanguage("PreferenceManage", "lbPreValueAdd", language, path);
            this.lbpreValueEn.Text = ManageSystemConfig.GetSystemSetLanguage("PreferenceManage", "lbpreValueEn", language, path);
            this.lbpreLevelEn.Text = ManageSystemConfig.GetSystemSetLanguage("PreferenceManage", "lbpreLevelEn", language, path);
            this.lbpreValueCn.Text = ManageSystemConfig.GetSystemSetLanguage("PreferenceManage", "lbpreValueCn", language, path);
            this.lbpreValueLevelAdd.Text = ManageSystemConfig.GetSystemSetLanguage("PreferenceManage", "lbpreValueLevelAdd", language, path);
            this.lbpreLevelCn.Text = ManageSystemConfig.GetSystemSetLanguage("PreferenceManage", "lbpreLevelCn", language, path);
            this.lbpreLevelDdl.Text = ManageSystemConfig.GetSystemSetLanguage("PreferenceManage", "lbpreLevelDdl", language, path);
            this.lbpreValueDdl.Text = ManageSystemConfig.GetSystemSetLanguage("PreferenceManage", "lbpreValueDdl", language, path);
            this.preTypeAddBtn.Text = ManageSystemConfig.GetSystemSetLanguage("PreferenceManage", "preTypeAddBtn", language, path);
            this.preTypeDelBtn.Text = ManageSystemConfig.GetSystemSetLanguage("PreferenceManage", "preTypeDelBtn", language, path);
            this.preValueAddBtn.Text = ManageSystemConfig.GetSystemSetLanguage("PreferenceManage", "preValueAddBtn", language, path);
            this.preValueDelBtn.Text = ManageSystemConfig.GetSystemSetLanguage("PreferenceManage", "preValueDelBtn", language, path);
            this.preValueLevelAddBtn.Text = ManageSystemConfig.GetSystemSetLanguage("PreferenceManage", "preValueLevelAddBtn", language, path);
            this.preValueLevelDelBtn.Text = ManageSystemConfig.GetSystemSetLanguage("PreferenceManage", "preValueLevelDelBtn", language, path);
            this.preTypeEditBtn.Text = ManageSystemConfig.GetSystemSetLanguage("PreferenceManage", "preTypeEditBtn", language, path);
            this.preValueEditBtn.Text = ManageSystemConfig.GetSystemSetLanguage("PreferenceManage", "preValueEditBtn", language, path);
            this.preValueLevelEditBtn.Text = ManageSystemConfig.GetSystemSetLanguage("PreferenceManage", "preValueLevelEditBtn", language, path);
        }

        private void SetFromImage()
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    this.iPreManage.ImageUrl = "images/title_PreferencesManage.jpg";
                    break;

                case "en":
                    this.iPreManage.ImageUrl = "images/title_PreferencesManageEn.jpg";
                    break;
            }
        }
    }
}

